
import unittest
import diabind
import testdata
import os

class SymMiscTest( unittest.TestCase ):

    def __get_glob(self):
        if not hasattr(self, "__glob"):
            self.__glob = testdata.loaded_session.get_globalScope()
        return self.__glob

    def __get_func(self, name):
        if not hasattr(self, "__method"):
            glob = testdata.loaded_session.get_globalScope()
            enum = self.__get_glob().findChildren( diabind.SymTagFunction,
                                                   name,
                                                   diabind.nsfUndecoratedName )
            self.assertEqual(len(enum), 1)
            self.__method = enum[0]
        return self.__method

    def __get_data(self, name):
        if not hasattr(self, "__data"):
            glob = testdata.loaded_session.get_globalScope()
            enum = self.__get_glob().findChildren( diabind.SymTagData,
                                                   name,
                                                   diabind.nsfUndecoratedName )
            self.assertEqual(len(enum), 1)
            self.__data = enum[0]
        return self.__data

    def test_lexicalParent(self):
        self.assertRaises( diabind.DiaError, 
                           self.__get_glob().get_lexicalParent )

        sym = self.__get_func("diabind::CLoader::FromExe").get_lexicalParent()
        self.assertEqual(sym.get_symTag(), diabind.SymTagCompiland)

        sym = sym.get_lexicalParent()
        self.assertEqual(sym.get_symTag(), diabind.SymTagExe)

    def test_type(self):
        sym = self.__get_func("diabind::CLoader::FromExe")
        self.assertEqual(sym.get_type().get_symTag(), diabind.SymTagFunctionType)

        sym = self.__get_data("CDiaErrorTranslate::g_pType")
        self.assertEqual(sym.get_type().get_symTag(), diabind.SymTagPointerType)

    def test_dataKind(self):
        sym = self.__get_func("diabind::CLoader::FromExe")
        self.assertRaises( diabind.DiaError, sym.get_dataKind )

        sym = self.__get_data("diabind::g_ComInitializer")
        self.assertEqual(sym.get_dataKind(), diabind.DataIsGlobal)

    def test_locationType(self):
        locs = []
        for sym in self.__get_func("diabind::CLoader::FromExe").findChildren(diabind.SymTagNull,
                                                                             "",
                                                                             diabind.nsNone ):
            try:
                locs.append(sym.get_locationType())
            except diabind.DiaError:
                pass
        self.assertTrue(diabind.LocIsStatic in locs)
        self.assertTrue(diabind.LocIsRegRel in locs)
        self.assertFalse(diabind.LocIsBitField in locs)
